package com.woniu.order.controller;


import cn.hutool.jwt.JWTUtil;
import com.woniu.core.util.JwtUtil;
import com.woniu.core.util.PageInfo;
import com.woniu.core.util.RedisUtil;
import com.woniu.core.util.Result;
import com.woniu.order.dto.AddGymEnterOrderDto;
import com.woniu.order.dto.AdminInOrderSearchDto;
import com.woniu.order.dto.AdminOrderSearchDto;
import com.woniu.order.form.AddGymEnterOrderForm;
import com.woniu.order.form.AdminInOrderSearchForm;
import com.woniu.order.form.AdminOrderSearchForm;
import com.woniu.order.form.OrderNumForm;
import com.woniu.order.service.TInOrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tInOrder")
public class TInOrderController {
    @Resource
    private TInOrderService tInOrderService;
    @Resource
    private RedisUtil redisUtil;
    @ApiOperation("新增场馆入驻订单表")
    @PostMapping("/addGymEnterOrder")
    public Result<AddGymEnterOrderDto> addGymEnterOrder(@RequestBody AddGymEnterOrderForm form){
        AddGymEnterOrderDto dto = tInOrderService.addGymEnterOrder(form);
        return Result.getResult(AddGymEnterOrderDto.class).setMsg("新增成功").setData(dto);
    }
    @PostMapping("/pay")
    @ApiOperation("支付订单接口")
    public Result pay(@RequestBody OrderNumForm orderForm)throws Exception{

        String form = tInOrderService.pay(orderForm);
        return Result.getResult(String.class).setMsg("发起支付成功").setData(form);

    }
    @PostMapping("/notify")
    @ApiOperation("异步回调接口")
    public String payNotify(HttpServletRequest request)throws Exception {
        //在控制层从request取出支付宝传过来的参数
        Map<String, String> params = new HashMap<String, String>();
        Map<String, String[]> requestParams = request.getParameterMap();
        for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {

            String name = (String) iter.next();

            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }

            params.put(name, valueStr);
        }
        tInOrderService.payNotify(params);
        return "SUCCESS";
    }

    @PostMapping("/listInOrder.admin")
    @ApiOperation("管理员分页查询或根据订单状态/场馆num/订单num查询所有入驻订单表")
    public Result<PageInfo> adminList(@RequestBody AdminInOrderSearchForm form) throws Exception {
        PageInfo<AdminInOrderSearchDto> search = tInOrderService.adminListInOrder(form);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(search);
    }

}

